package com.rongke.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rongke.enums.MsgTypeEnum;
import com.rongke.enums.OrderStatusEnum;
import com.rongke.mapper.ExperienceGoodsMapper;
import com.rongke.model.*;
import com.rongke.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @version : Ver 1.0
 * @ExperienceGoodsServiceImpl
 * @体验众测ServiceImpl
 */
@Service
public class ExperienceGoodsServiceImpl extends ServiceImpl<ExperienceGoodsMapper, ExperienceGoods> implements ExperienceGoodsService {

    @Autowired
    private ExperienceGoodsMapper experienceGoodsMapper;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderGoodsService orderGoodsService;
    @Autowired
    private GoodsQuerstionService goodsQuerstionService;
    @Autowired
    private ExperienceGoodsService experienceGoodsService;
    @Autowired
    private MsgUserService msgUserService;
    @Autowired
    private MessageService messageService;

    /**
     * @param experienceGoods
     * @return
     * @根据条件查找体验众测
     */
    @Override
    public List<ExperienceGoods> selectByCondition(ExperienceGoods experienceGoods) {
        return experienceGoodsMapper.selectByCondition(experienceGoods);
    }

    @Override
    public List<ExperienceGoods> selectExpGoodsList(Map map) {

        return experienceGoodsMapper.selectExpGoodsList(map);
    }

    @Override
    public void noticeToFillQuestionaire() {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-15);//15天
        Date fifteenDaysAgo=cal.getTime();
        //查找试用众测收货15天后还未填写调查问卷的订单
        EntityWrapper<Orders> ew = new EntityWrapper<>();
        ew.eq("order_type", 2)
                .eq("is_noticed", 0)//尚未提醒
                .eq("order_status", OrderStatusEnum.PENDING_EVALUATION.getType())
                .le("complete_datetime",fifteenDaysAgo);
        List<Orders> expOrders=ordersService.selectList(ew);
        if(!expOrders.isEmpty()){
            expOrders.forEach(e->{
                EntityWrapper<OrderGoods> ewog=new EntityWrapper<>();
                ewog.eq("order_id",e.getId());
                OrderGoods og=orderGoodsService.selectOne(ewog);
                if(null!=og){
                    EntityWrapper<ExperienceGoods> ewe=new EntityWrapper<>();
                    ewe.eq("goods_id",og.getGoodsId());
                    EntityWrapper<GoodsQuerstion> ewgq=new EntityWrapper<>();
                    ewgq.eq("goods_id",og.getGoodsId());
                    //仍在试用众测商品中并有调查问卷
                    if(null!=experienceGoodsService.selectOne(ewe)&&null!=goodsQuerstionService.selectOne(ewgq)){

                        //将该订单问卷提醒状态改为 1 已提醒
                        Orders newOrder=new Orders(e.getId());
                        newOrder.setIsNoticed(1);
                        ordersService.updateById(newOrder);

                        /******* 试用问卷填写提醒提示消息******/
                        Message msg=messageService.selectTemplateMsgByMsgType(5);
                        if(null!=msg){
                            String title=msg.getTitle();
                            String content=msg.getContent()
                                    .replace("#orderNumber#",e.getOrderNumber());
                            MsgUser msgUser=new MsgUser();
                            msgUser.setMuserId(e.getMuserId());
                            msgUser.setTitle(title);
                            //你试用的订单号为【 #orderNumber# 】的试用众测订单,已收货15天未填写调查问卷，为了不影响试用信誉，请及时到【我的】->【我的体验】中填写问卷
                            msgUser.setMsgContent(content);
                            msgUserService.insert(msgUser);
                        }

                        /******* 试用问卷填写提醒提示消息******/
                    }
                }
            });
        }
    }

    /** 购买试用商品支付时检查该用户是否有收货后一定天数未填写调查的订单
     * @return 返回true表示此次不能购买
     */
    @Override
    public boolean ifLeftOversExpOrdersExist(Long muserId) {
        Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-15);//15天,可能变更
        Date fifteenDaysAgo=cal.getTime();
        //查找试用众测收货15天后还未填写调查问卷的订单
        EntityWrapper<Orders> ew = new EntityWrapper<>();
        ew.eq("order_type", 2)
                .eq("muser_id",muserId)
                .eq("order_status", OrderStatusEnum.PENDING_EVALUATION.getType())
                .le("complete_datetime",fifteenDaysAgo);
        List<Orders> expOrders=ordersService.selectList(ew);
        if(!expOrders.isEmpty()){
                for(Orders e:expOrders){
                    EntityWrapper<OrderGoods> ewog=new EntityWrapper<>();
                    ewog.eq("order_id",e.getId());
                    OrderGoods og=orderGoodsService.selectOne(ewog);
                    if(null!=og){
                        EntityWrapper<ExperienceGoods> ewe=new EntityWrapper<>();
                        ewe.eq("goods_id",og.getGoodsId());
                        EntityWrapper<GoodsQuerstion> ewgq=new EntityWrapper<>();
                        ewgq.eq("goods_id",og.getGoodsId());
                        //仍在试用众测商品中并有调查问卷
                        if(null!=experienceGoodsService.selectOne(ewe)&&null!=goodsQuerstionService.selectOne(ewgq)){
                            return true;
                        }
                    }
                }
        }
        return false;
    }


}
